package org.amphiprion.gameengine;

import org.amphiprion.gameengine.util.TextureHelper;

import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;
import android.content.pm.ConfigurationInfo;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.Window;
import android.view.WindowManager;

public class GameActivity extends Activity {
	public static Context context;
	protected GameView view;
	private int referenceWidth;
	private int referenceHeight;

	public GameActivity(int referenceWidth, int referenceHeight) {
		this.referenceWidth = referenceWidth;
		this.referenceHeight = referenceHeight;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE); // (NEW)
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); // (NEW)

		GameActivity.context = this;
		view = new GameView(this, referenceWidth, referenceHeight);

		// Check if the system supports OpenGL ES 2.0.
		final ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
		final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();
		final boolean supportsEs2 = configurationInfo.reqGlEsVersion >= 0x20000;

		if (supportsEs2) {
			// Request an OpenGL ES 2.0 compatible context.
			view.setEGLContextClientVersion(2);

			// Set the renderer to our demo renderer, defined below.
			view.setRenderer(new GameRenderer(this, referenceWidth, referenceHeight, view));
		} else {
			// This is where you could create an OpenGL ES 1.x compatible
			// renderer if you wanted to support both ES 1 and ES 2.
			return;
		}

		setContentView(view);
	}

	@Override
	protected void onResume() {
		// The activity must call the GL surface view's onResume() on activity onResume().
		super.onResume();
		view.onResume();
	}

	@Override
	protected void onPause() {
		// The activity must call the GL surface view's onPause() on activity onPause().
		super.onPause();
		view.onPause();
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		view.onTouch(event);
		return true;
	}

	@Override
	protected void onDestroy() {
		TextureHelper.unloadAll();
		super.onDestroy();

	}

	@Override
	public void onBackPressed() {
		if (view.backRequested()) {
			super.onBackPressed();
		}
	}
}
